import moment from 'moment';

/** 处理在线编辑数据 */
export const wordHandChildParam = (params: Record<string, any>) => {
  if (params && JSON.stringify(params) != '{}') {
    return JSON.stringify(params)?.replace(/\"/g, "'");
  } else {
    return '';
  }
};

/** 处理合同在线编辑数据 */
export const wordHandChildParamContract = (urlObj: Record<string, any>) => {
  let payLoad = {
    ...urlObj,
  };
  //相对方单位名称
  if (urlObj?.oppositeList) {
    let supplyName: any = [];
    urlObj?.oppositeList.map((item) => {
      supplyName.push(item?.oppositeName);
    });
    payLoad['supplyNames'] = supplyName.join(',');
  }
  //资金流向
  payLoad['capitalDirName'] = urlObj?.capitalDir == 0 ? '付款' : urlObj?.capitalDir == 1 ? '收款' : urlObj?.capitalDir == 2 ? '无金额' : '';
  //是否重大（要）合同
  payLoad['majorFlagName'] = urlObj?.majorFlag == 0 ? '否' : '是';
  //预计起始时间
  payLoad['preBeginTime'] = urlObj?.preBeginTime ? moment(urlObj?.preBeginTime)?.format('YYYY-MM-DD HH:mm:ss') : '';
  //预计结束时间
  payLoad['preFinishTime'] = urlObj?.preFinishTime ? moment(urlObj?.preFinishTime)?.format('YYYY-MM-DD HH:mm:ss') : '';
  //中标时间
  payLoad['bidTime'] = urlObj?.bidTime ? moment(urlObj?.bidTime)?.format('YYYY-MM-DD') : '';
  //是否超预算
  payLoad['budgetFlag'] = urlObj?.budgetFlag == 0 ? '否' : '是';
  //是否补录
  payLoad['recordFlag'] = urlObj?.recordFlag == 0 ? '否' : '是';
  //是否使用范本
  payLoad['templateFlag'] = urlObj?.templateFlag == 0 ? '否' : '是';

  return payLoad;
};

/**处理在线编辑路由携带参数 */
export const ntkoPageUrlParams = (params: Record<string, any>) => {
  let paramStr = '';
  for (let key in params) {
    paramStr += `${key}:${params[key]},`;
  }
  if (paramStr) {
    return paramStr.substring(0, paramStr.length - 1);
  } else {
    return '';
  }
};

/** 向子页面传值 */
export const wordToChildParams = (wordDoc: any, childUrl: string, childPayload: any) => {
  const childData: string = wordHandChildParam(childPayload);
  wordDoc?.ntkoSendDataToChild(childUrl, childData);
};
